package models

import (
	"registration-booking/global"
	"time"
)

type TestProject struct {
	ID
	ProjectName string `json:"projectName" gorm:"comment:体检项目"`
	Price       string `json:"price" gorm:"comment:价格"`
	Description string `json:"description" gorm:"comment:描述"`
	ParentId    uint   `json:"parent_id" gorm:"comment:父级id"`
	Timestamps
	SoftDeletes
}

func (testProject *TestProject) Update() error {
	testProject.UpdatedAt = time.Now()
	return global.App.DB.Updates(testProject).Error
}

func (testProject *TestProject) Create() error {
	testProject.CreatedAt = time.Now()
	testProject.UpdatedAt = time.Now()
	return global.App.DB.Create(testProject).Error
}

func DeleteTestProjectById(id int) error {
	return global.App.DB.Model(TestProject{}).Delete("id = ?", id).Error
}

func FindTestProjectById(id uint) (testProject TestProject, err error) {
	err = global.App.DB.Where("id = ?", id).First(&testProject).Error
	return
}

func FindProjects(pageNo, pageSize int) (testProjects []TestProject, count int64, err error) {
	err = global.App.DB.Model(TestProject{}).Where("parent_id != 0").Count(&count).Offset((pageNo - 1) * pageSize).Limit(pageSize).Find(&testProjects).Error
	return
}

func FindParentProjects() (testProjects []TestProject, count int64, err error) {
	err = global.App.DB.Model(TestProject{}).Where("parent_id = 0").Count(&count).Find(&testProjects).Error
	return
}
